package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @author ZhangYuJie
 * projectName:shop_manage
 * time:2022/5/17 20:28
 * description:
 */
@Service
public class DtsStorageServiceImpl implements DtsStorageService {


    @Autowired
    private DtsStorageMapper storageMapper;


    //OSS服务器访问域名
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    //子账户名
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    //子账户密码
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    //桶名字
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;


    @Override
    public IPage<DtsStorage> querySelective(String key, String name, Integer page, Integer limit, String sort, String order) {
        IPage<DtsStorage> iPage = new Page<>(page, limit);

        QueryWrapper<DtsStorage> wapper = new QueryWrapper<>();

        wapper.eq("deleted", false);

        if (!StringUtils.isEmpty(key)) {
            wapper.eq("`key`", key);
        }
        if (!StringUtils.isEmpty(name)) {
            wapper.like("name", name);
        }

        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                wapper.orderByAsc(sort);
            } else {
                wapper.orderByDesc(sort);
            }

        }

        iPage = storageMapper.selectPage(iPage, wapper);

        return iPage;
    }

    @Override
    public DtsStorage add(String name, int size, String ext, byte[] content) throws Exception {
        String key = UUID.randomUUID().toString();

        //将图片上传到阿里云OSS
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, key + ext, content);

        //创建数据库的文件存储对象
        DtsStorage storage = new DtsStorage();
        storage.setDeleted(false);
        storage.setAddTime(new Date());
        //文件唯一标识
        storage.setKey(key + ext);
        //文件名
        storage.setName(name);
        //扩展名
        storage.setType(ext);
        //文件大小
        storage.setSize(size);
        storage.setUpdateTime(new Date());
        //文件存放地址
        storage.setUrl(url);
        storageMapper.insert(storage);

        return storage;
    }

    @Override
    public int update(DtsStorage dtsStorage) {
        dtsStorage.setUpdateTime(new Date());
        return storageMapper.updateById(dtsStorage);
    }

    @Override
    public void deleteByKey(String key) {
        QueryWrapper<DtsStorage> wapper = new QueryWrapper<>();
        wapper.eq("`key`", key);

        //阿里云中删除文件
        AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName,key);
        //数据库中删除文件
        storageMapper.delete(wapper);
    }

    @Override
    public DtsStorage findByKey(String key) {
        QueryWrapper<DtsStorage> wapper = new QueryWrapper<>();
        wapper.eq("`key`", key);
        wapper.eq("deleted", false);
        DtsStorage one = storageMapper.selectOne(wapper);
        return one;
    }

    @Override
    public DtsStorage findById(Integer id) {
        return storageMapper.selectById(id);
    }
}
